package com.resou.reader.data;

import android.content.Context;

import com.google.gson.GsonBuilder;
import com.resou.reader.api.ApiConstant;
import com.resou.reader.api.service.ActivityService;
import com.resou.reader.api.service.BookService;
import com.resou.reader.api.service.PayService;
import com.resou.reader.api.service.SearchService;
import com.resou.reader.api.service.UserService;
import com.resou.reader.data.bookdetail.BookDetailRepository;
import com.resou.reader.data.bookshelf.BookshelfRepository;
import com.resou.reader.data.bookshelf.BookshelfService;
import com.resou.reader.data.bookstore.BookStoreRepository;
import com.resou.reader.data.bookstore.BookStoreService;
import com.resou.reader.data.gson.IgnoreExclusionStrategy;
import com.resou.reader.data.readhistory.ReadHistoryRepository;
import com.resou.reader.data.readhistory.ReadRecordService;
import com.resou.reader.data.signin.SignInRepository;
import com.resou.reader.data.signin.SignInService;
import com.resou.reader.data.tinder.TinderRepository;
import com.resou.reader.data.tinder.local.TinderLocalDataSource;
import com.resou.reader.data.tinder.remote.TinderRemoteDataSource;
import com.resou.reader.data.vip.VipService;
import com.resou.reader.reading.ReadingRepository;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class Injection {
    /**
     * 提供TinderRepository实例
     *
     * @param context Context对象
     * @return
     */
    public static TinderRepository provideTinderRepository(Context context) {
        return TinderRepository.getInstance(
                TinderLocalDataSource.getInstance(context)
                , TinderRemoteDataSource.getInstance()
        );
    }

//    public static OkHttpClient provideOkHttpClient() {
//        //http日志拦截器
//        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//        //需要打印出的日志级别
//        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//        return new OkHttpClient.Builder()
//                .addInterceptor(interceptor)
//                .build();
//    }

    public static Retrofit provideRetrofit() {
        return new Retrofit.Builder().baseUrl(ApiConstant.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(provideOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 提供各种Repository
     *
     * @return
     */
    public static BookshelfRepository provideBookshelfRepository() {
        return BookshelfRepository.getInstance(provideBookshelfService());
    }

    public static BookStoreRepository provideBookstoreRepository() {
        return BookStoreRepository.getInstance(provideStoreService());
    }

    public static SignInRepository provideSignInRepository() {
        return SignInRepository.getInstance(provideSignInService());
    }

    public static ReadingRepository provideReadingRepository() {
        return ReadingRepository.getInstance();
    }

    public static ReadHistoryRepository provideReadHistoryRepository() {
        return ReadHistoryRepository.getInstance();
    }

    public static BookDetailRepository provideBookDetailRepository() {
        return BookDetailRepository.getInstance();
    }

    /**
     * 提供HTTP客户端
     *
     * @return
     */
    public static OkHttpClient provideOkHttpClient() {
        //http日志拦截器
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        //需要打印出的日志级别
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .connectTimeout(15, TimeUnit.SECONDS)//15秒连接超时
                .writeTimeout(15, TimeUnit.SECONDS)//15m秒写入超时
                .readTimeout(15, TimeUnit.SECONDS)//15秒读取超时
                .build();
    }

    /**
     * 提供各种service
     *
     * @return
     */
    //提供书架
    public static BookshelfService provideBookshelfService() {
        return provideReleaseRetrofit().create(BookshelfService.class);
    }

    public static BookService provideBookService() {
        return provideReleaseRetrofit().create(BookService.class);
    }

    public static PayService providePayService() {
        return provideReleaseRetrofit().create(PayService.class);
    }

    public static SearchService provideSearchService() {
        return provideReleaseRetrofit().create(SearchService.class);
    }

    public static BookStoreService provideStoreService() {
        return provideReleaseRetrofit().create(BookStoreService.class);
    }

    public static UserService provideUserService() {
        return provideReleaseRetrofit().create(UserService.class);
    }

    public static SignInService provideSignInService() {
        return provideReleaseRetrofit().create(SignInService.class);
    }

    public static VipService provideVipService() {
        return provideReleaseRetrofit().create(VipService.class);
    }

    public static ReadRecordService provideReadRecordService() {
        return provideReleaseRetrofit().create(ReadRecordService.class);
    }

    public static Retrofit provideReleaseRetrofit() {
        return new Retrofit.Builder().baseUrl(ApiConstant.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(provideOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create(
                        new GsonBuilder()
                                .setExclusionStrategies(new IgnoreExclusionStrategy(Long.class))
                                .create()))
                .build();
    }

    public static ActivityService provideActivityService() {
        return provideReleaseRetrofit().create(ActivityService.class);
    }
}
